package game;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

import gfx.Camera;
import gfx.SurfaceView;
import util.GameClock;
import util.GameVars;
import util.InputHandler;
import util.Logger;

public class FullscreenActivity extends Activity {
	
	private SurfaceView GLView;
	private InputHandler inputHandler;
	private Game game;
	private AlertDialog ad;
	AlertDialog.Builder alertDialogBuilder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Logger.log("Activity: Created");
		super.onCreate(savedInstanceState);

		game = new Game();
		game.setContext(this);
		
		GLView = new SurfaceView(this, game);
		
		alertDialogBuilder = new AlertDialog.Builder(this);

		inputHandler = InputHandler.getInstance();
		inputHandler.init(this, game);

		setContentView(GLView);
		GLView.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent e) {
				try {
					inputHandler.handleTouchEvent(e);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
				return true;
			}
		});
		
		// Set up the user interaction to manually show or hide the system UI.
//		contentView.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//			}
//		});
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		float width = 0;
		float height = 0;
		
		//This is to flip the screen //zachs bug if you start game with screen off
		if(metrics.widthPixels > metrics.heightPixels){
			width = metrics.widthPixels;
			height = metrics.heightPixels;
		}else{
			width = metrics.heightPixels;
			height = metrics.widthPixels;
		}
		
		Camera.getInstance().setScreenSize((int)width,(int)height);
	}
	
	public void onBackPressed(){
		if(game.isReady())
			if(game.handleBackPress())
				finish();
		// TODO Change to confirm / stuff
//		exit();
	}
	
	protected void onPause(){
		if(game != null){
			game.pause();
		}
		
		GameClock.getInstance().pause();
		
//		if(game != null)
//			if(game.getStage() != null)
//				game.getStage().pause();
		super.onPause();
	}
	
	protected void onResume(){
		if(game != null){
			if(game.getStage() != null)
				if(!game.getStage().isPaused())
						GameClock.getInstance().resume();
		}
//		if(game != null)
//			if(game.getStage() != null)
//				game.getStage().resume();
		super.onResume();
	}
	
	private void exit(){
		kill();
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
	}
	
	public void kill(){
		onPause();
//		
//		alertDialogBuilder.setTitle("Really Exit?");
// 
//		alertDialogBuilder.setMessage("We Love You!  :<").setCancelable(false).setPositiveButton("Exit",new DialogInterface.OnClickListener() {
//			public void onClick(DialogInterface dialog,int id) {
//				finish();
//			}
//		}).setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
//			public void onClick(DialogInterface dialog,int id) {
//				dialog.cancel();
//				onResume();
//			}
//		});
// 
//		ad = alertDialogBuilder.create();
//		
//		ad.show();
		
		//
//		finish();
	}
	
	public void quickKill(){
		finish();
	}

	public static Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) { 
			super.handleMessage(msg);
			String text = msg.getData().getString("Text");
			Logger.popUp(text);
		}
	};
}
